package com.supereye.platform.web.terminal.service;

import java.io.File;
import java.io.InputStream;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import javax.annotation.Resource;

import org.codehaus.jackson.type.TypeReference;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.druid.util.StringUtils;
import com.supereye.platform.common.PageData;
import com.supereye.platform.util.BeanUtil;
import com.supereye.platform.util.ExcelUtil;
import com.supereye.platform.util.JacksonUtil;
import com.supereye.platform.util.PropUtil;
import com.supereye.platform.web.terminal.dao.TerminalDao;
import com.supereye.platform.web.terminal.model.TopTerminal;

@Service
public class TerminalService {
	@Resource
	private TerminalDao terminalDao;
	
	public void addTerminal(TopTerminal terminal) throws Exception {
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		terminal.setCreateDate(format.format(new Date()));
		terminalDao.addTerminal(terminal);
	}

	public String getTerminalList(Integer pageNum,Integer pageSize,TopTerminal terminal) throws Exception {
		Map<String,Object> params = BeanUtil.beanToMap( terminal);
		Integer count = terminalDao.getTerminalListCount(params);
		
		params.put("pageFlag", "1");
		params.put("fromNum", (pageNum-1)*pageSize);
		params.put("pageSize", pageSize);
		List<TopTerminal> list = terminalDao.getTerminalList(params);
		
		PageData<TopTerminal> pageData = new PageData<TopTerminal>();
		pageData.setTotal(count);
		pageData.setRows(list);
		return JacksonUtil.toJSon(pageData);
	}

	public void deleteTerminals(String[] terminalIds) throws Exception {
		terminalDao.deleteTerminals(terminalIds);
	}

	public void updateTerminal(TopTerminal terminal) throws Exception {
		terminalDao.updateTerminal(terminal);
	}

	public String getTerminalForCombo() throws Exception {
		List<TopTerminal> list = terminalDao.getTerminalList(new HashMap<String,Object>());
		return JacksonUtil.toJSon(list);
	}

	public String exportTerminals(MultipartFile file) throws Exception {
		String tmpPath = PropUtil.get("upload.tmp.url");
		String path=tmpPath+new Date().getTime()+"_"+file.getOriginalFilename();
		if(StringUtils.isEmpty(file.getOriginalFilename())){
			throw new Exception("请选择文件");
		}
		if(!file.getOriginalFilename().toLowerCase().endsWith(".xlsx") && !file.getOriginalFilename().toLowerCase().endsWith(".xls")){
			throw new Exception("文件后缀名为.xlsx或者.xls");
		}
        File newFile=new File(path);
        if(!new File(tmpPath).isDirectory()){
        	new File(tmpPath).mkdirs();
        }
        file.transferTo(newFile);
        
        //读取临时文件的EXCEL文件
        List<Map<Integer,Object>> list = ExcelUtil.readExcel(path);
        
        File usedFile = new File(path);
        usedFile.delete();
        
        List<TopTerminal> result = new ArrayList<TopTerminal>();
        TopTerminal terminal = null;
        InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("terminal.properties");
        Properties pro = new Properties();
        pro.load(in);
		in.close();
		
        for(Map<Integer,Object> map:list){
        	terminal = new TopTerminal();
        	terminal.setMac(getStringValue(map,0));
        	
        	terminal.setBrand(pro.getProperty("brand"));
        	terminal.setTerminalVersion(pro.getProperty("terminal_version"));
        	terminal.setNetVersion(pro.getProperty("net_version"));
        	terminal.setLaunchYear(pro.getProperty("launch_year"));
        	terminal.setLaunchMonth(pro.getProperty("launch_month"));
        	terminal.setWidth(pro.getProperty("width"));
        	terminal.setLength(pro.getProperty("length"));
        	terminal.setThick(pro.getProperty("thick"));
        	terminal.setWeight(pro.getProperty("weight"));
        	terminal.setColor(pro.getProperty("color"));
        	terminal.setBodyMaterialCatergory(pro.getProperty("body_material_catergory"));
        	terminal.setHolderMaterialCatergory(pro.getProperty("holder_material_catergory"));
        	terminal.setHeaderMaterialCatergory(pro.getProperty("header_material_catergory"));
        	terminal.setOperatingSystem(pro.getProperty("operating_system"));
        	terminal.setOperatingSystemVersion(pro.getProperty("operating_system_version"));
        	terminal.setRom(pro.getProperty("rom"));
        	terminal.setRam(pro.getProperty("ram"));
        	terminal.setCpuBrand(pro.getProperty("cpu_brand"));
        	terminal.setCpuFrequency(pro.getProperty("cpu_frequency"));
        	terminal.setCpuCoreNum(pro.getProperty("cpu_core_num"));
        	terminal.setCpuVersion(pro.getProperty("cpu_version"));
        	terminal.setMaxExtCapacity(pro.getProperty("max_ext_capacity"));
        	terminal.setCameraNum(pro.getProperty("camera_num"));
        	terminal.setCameraPixel(pro.getProperty("camera_pixel"));
        	terminal.setLensSize(pro.getProperty("lens_size"));
        	terminal.setBatteryCapacity(pro.getProperty("battery_capacity"));
        	terminal.setScreenFbl(pro.getProperty("screen_fbl"));
        	terminal.setAwaitRunTime(pro.getProperty("await_run_time"));
        	terminal.setScreenDensity(pro.getProperty("screen_density"));
        	terminal.setAvailRunTime(pro.getProperty("avail_run_time"));
        	terminal.setDataTransInterface(pro.getProperty("data_trans_interface"));
        	terminal.setPackingList(pro.getProperty("packing_list"));
        	
        	terminal.setOperatingSystemVersion(pro.getProperty("operating_system_version"));
        	
        	result.add(terminal);
        }
        
        PageData<TopTerminal> pageData = new PageData<TopTerminal>();
        pageData.setRows(result);
        return JacksonUtil.toJSon(pageData);
	}
	
	public String getStringValue(Map<Integer,Object> map,Integer key){
		if(map!=null){
			return String.valueOf(map.get(key));
		}else{
			return null;
		}
	}
	
	public Integer getIntgerValue(Map<Integer,Object> map,Integer key){
		try{
			if(map!=null){
				String value = (String) map.get(key);
				BigDecimal bd = new BigDecimal(value);
				return bd.intValue();
			}else{
				return null;
			}
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
		
	}

	public void confirmTempTerminals(String terminalListString) throws Exception {
		List<TopTerminal> list = JacksonUtil.readValue(terminalListString, new TypeReference<List<TopTerminal>>(){});
		for(TopTerminal termianl:list){
			this.addTerminal(termianl);
		}
	}
}
